package com.chennan.rebbitmq.uitl;

import com.chennan.rebbitmq.ServiceConstant;
import com.chennan.rebbitmq.constant.RabbitMQExchangeEnum;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author chennan
 * @create 2021-06
 */
public class MqConnection {

    public static Connection getConnection() throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2、设置连接属性
        factory.setHost(ServiceConstant.IP);
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("a111111");

        Connection connection = null;
        // 3、从连接工厂获取连接
        connection = factory.newConnection("生产者");

        return connection;
    }

    /**
     * 声名交换机
     * @param channel 通道
     * @param exchangeName 交换机名称
     * @param rabbitMQExchangeEnum 交换机类型
     */
    public static void exchangeDeclare(Channel channel, String exchangeName, RabbitMQExchangeEnum rabbitMQExchangeEnum){
        try {
            AMQP.Exchange.DeclareOk declareOk = channel.exchangeDeclare(exchangeName, rabbitMQExchangeEnum.getType());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("报错了。。。。");
        }
    }

    /**
     * 声明队列
     * @param channel 通道
     * @param queueName 队列名称
     */
    public static void queueDeclare(Channel channel, String queueName){
        try {
            AMQP.Queue.DeclareOk declareOk = channel.queueDeclare(queueName, false, false, false, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 交换机和队列绑定 
     * @param exchangeName 交换机名称
     * @param queueName 队列名称
     * @param routingKey 匹配路由
     */
    public static void queueBind(Channel channel,String exchangeName,String queueName,String routingKey){
        try {
            AMQP.Queue.BindOk bindOk = channel.queueBind(queueName, exchangeName, routingKey);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建消费者
     * @param channel 通道
     * @return 消费者
     */
    public static Consumer createDefaultConsumer(Channel channel){
        return new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                super.handleDelivery(consumerTag, envelope, properties, body);
                String msg = new String(body,"UTF-8");
                System.out.println("接受到消息："+msg);
            }
        };
    }

    /**
     * 消费消息
     * @param queueName 队列名称
     * @param autoAck 是否自动ack
     * @param consumer 消费者
     */
    public static void basicConsume(Channel channel,String queueName,boolean autoAck,Consumer consumer){
        try {
            String s = channel.basicConsume(queueName, autoAck, consumer);
            System.out.println("消费后返回："+s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生产消息
     * @param channel 通道
     * @param exchangeName 交换机名称
     * @param routingKey 路由key
     * @param msg 消息
     */
    public static void basicPublish(Channel channel,String exchangeName,String routingKey,String msg){
        try {
            channel.basicPublish(exchangeName,routingKey,null,msg.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void closeRabbitMQ(Connection connection,Channel channel){

    }


}
